package com.loong.data.consumer;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.loong.common.rabbitmq.consumer.BaseConsumer;
import com.loong.common.rabbitmq.dto.GoodsSpecificationDTO;
import com.loong.common.rabbitmq.dto.base.RepositoryEventDto;
import com.loong.data.handler.GoodsSpecificationHandler;
import com.loong.global.constant.MQConstant;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.io.IOException;

/**
 * @Description:
 * @Author: hzp
 * @Date: 2020-12-5 9:43
 */
@Slf4j
@Component
public class GoodsSpecificationReportConsumer extends BaseConsumer {
    @Resource
    private GoodsSpecificationHandler goodsSpecificationHandler;


    @RabbitListener(queues = {MQConstant.BASE_PREFIX+MQConstant.QUEUE_NAME.GOODS_SPECIFICATION+MQConstant.BASE_SUFFIX.QUEUE}, containerFactory = "defaultRabbitListener")
    public void receiveEvent(Channel channel, Message message) throws IOException {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        byte[] data = message.getBody();
        // 消费到商家商品规格修改
        log.debug("消费到数据：{}",new String(data));
        RepositoryEventDto<JSONObject> eventDto = JSON.parseObject(new String(data)).toJavaObject(RepositoryEventDto.class);
        Short eventCode = eventDto.getEventCode();
        GoodsSpecificationDTO goodsSpecificationDTO = eventDto.getData().toJavaObject(GoodsSpecificationDTO.class);
        switch (eventCode){
            case 1:
                goodsSpecificationHandler.saveGoodsSpecificationModel(goodsSpecificationDTO);
                break;
            case 2:
                goodsSpecificationHandler.updateGoodsSpecificationModel(goodsSpecificationDTO);
                break;
            case 3:
                goodsSpecificationHandler.deleteGoodsSpecificationModelByIds(goodsSpecificationDTO.getIds());
                break;
        }
        // 存储到ES
        submitAck(channel,deliveryTag);
    }
}
